# Author: Gregory Smith
# Date: 12 - 3 - 2018
# Title: Smith 2018 Secret But Constrained Final Model Functions

# Job: These functions save regression output as dataframes and include the 
#  bootstrapped clustered standard errors (relies upon  boot.clst.ses)

# Notes:
# This script is necessary for the other replication files to work correctly 
# For clstr_se_clean to work, output.replicates must be FALSE 

# Change Working Directory to Replicate Code 
setwd("~/Dropbox/covert_ops_paper/2019-smith-io-secret-but-constrained-replication-files/")

#---- Packages 

list.of.packages <- c("tidyverse", "texreg", "broom", "glue")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

#--- Plots and Data Manipulation 
library(tibble)
library(texreg)
library(broom)
library(glue)

#------ Model Output Functions (Cleans and Saves the Output)

clstr_se_clean <- function(cse_object, original_model, model_name) {
  
  require(glue)
  
  cse_object_df <- data.frame(cse_object) %>% tibble::rownames_to_column(var = "term")
  names(cse_object_df)[names(cse_object_df) == "clustered.bootstrap.p.value"] <- "clst_p.value"
  names(cse_object_df)[names(cse_object_df) == "ci.CI.lower"] <- "ci_lower"
  names(cse_object_df)[names(cse_object_df) == "ci.CI.higher"] <- "ci_upper"
  
  tidy_model <- broom::tidy(original_model)
  tidy_model <- merge(tidy_model, cse_object_df, by = "term")
  
  
  model_gof <- broom::glance(original_model) %>%
    gather(key = "gof_stat", value = "value", null.deviance:df.residual)
  
  n_df <- data.frame(gof_stat = "n_obs", value = nobs(original_model))
  model_gof  <- rbind(model_gof, n_df)
  
 #  model_path_ext <- glue('~/Dropbox/covert_ops_paper/io_resubmit/final_analysis/2018-conditional-accept-files/smith-2018-tidy-model-output/{model_name}_tidy_df.csv')
  
  model_path_ext <- glue('smith-2019-tidy-model-output/{model_name}_tidy_df.csv')
  
  
  write_csv(tidy_model, path = model_path_ext)
  model_path_warn <- glue("Note, {model_name} saved to: {model_path_ext}")
  print(model_path_warn)
  
 # gof_path_ext <- glue('~/Dropbox/covert_ops_paper/io_resubmit/final_analysis/2018-conditional-accept-files/smith-2018-gof-statistics/{model_name}_gof.csv')
  gof_path_ext <- glue('smith-2019-gof-statistics/{model_name}_gof.csv')
  
  write_csv(model_gof, path = gof_path_ext)
  gof_path_warn <- glue("Note, {model_name}_gof saved to: {gof_path_ext}")
  print(gof_path_warn)
  
}

#------ Converts Tidy Model Output into Texreg Objects  

tex_reg_create <- function(index_id, nested_df = final_dta) {
  
  coefficient.names <- nested_df$tidy_models[[index_id]]$term
  coefficients <- nested_df$tidy_models[[index_id]]$estimate
  ci.lower <- nested_df$tidy_models[[index_id]]$ci_lower
  ci.upper <- nested_df$tidy_models[[index_id]]$ci_upper
  se <- nested_df$tidy_models[[index_id]]$std.error
  significance <- nested_df$tidy_models[[index_id]]$clst_p.value
  
  lik <- nested_df$gof_stats[[index_id]]$value[[3]]
  aic <- nested_df$gof_stats[[index_id]]$value[[4]]
  bic <- nested_df$gof_stats[[index_id]]$value[[5]]
  n <-  nested_df$gof_stats[[index_id]]$value[[8]]
  gof <- c(aic, bic, lik, n) # Creates a vector of GOF statistics
  gof.names <- c("AIC", "BIC", "Log Likelihood", "Num. Obs.") #  #names of GOFs
  decimal.places <- c(TRUE, TRUE, TRUE, FALSE) # , FALSE #the last one is a count variable
  
  
  tex_reg_object <- createTexreg( # creates texreg object
    coef.names = coefficient.names,
    coef = coefficients,
    ci.low = ci.lower,
    ci.up = ci.upper,
    se = se,
    pvalues = significance,
    gof.names = gof.names,
    gof = gof,
    gof.decimal = decimal.places
    
  )
  
  tr_obj_name <- paste0(nested_df$model_name[[index_id]], ".tr")
  assign(tr_obj_name, tex_reg_object, envir = .GlobalEnv)
  
} 


